package com.bj58.wuxian.lucene.query;

import java.io.IOException;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TermRangeQuery;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.store.Directory;
import org.junit.Test;

import com.bj58.wuxian.lucene.constants.Constants;
import com.bj58.wuxian.lucene.utils.LuceneUtil;

public class Queryer {

	@Test
	public void testTermQuery(){
		Directory directory =null;
		DirectoryReader reader =null;
		try {
			  directory = LuceneUtil.getDirectory(Constants.DIRECTROY_PATH);
			  reader = LuceneUtil.getDirectoryReader(directory);
			IndexSearcher indexSearcher = LuceneUtil.getIndexSearcher(reader);
			Term term=new Term("title", "三国演义1");
			TermQuery query=new TermQuery(term);
			ScoreDoc[] hitDocs = indexSearcher.search(query, 10).scoreDocs;
			for (int i = 0; i < hitDocs.length; i++) {
				Document hitDoc = indexSearcher.doc(hitDocs[i].doc);
				System.out.println("id： " + hitDoc.get("id") + "  title:" + hitDoc.get("title") + "   author:"
						+ hitDoc.get("author") + "   desc:" + hitDoc.get("desc"));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			LuceneUtil.close(reader, directory);
		}
	}
	
	@Test
	public void testPhraseQuery(){
		Directory directory =null;
		DirectoryReader reader =null;
		try {
			  directory = LuceneUtil.getDirectory(Constants.DIRECTROY_PATH);
			  reader = LuceneUtil.getDirectoryReader(directory);
			IndexSearcher indexSearcher = LuceneUtil.getIndexSearcher(reader);
			PhraseQuery query =  new PhraseQuery(2,"desc","三","义");
			ScoreDoc[] hitDocs = indexSearcher.search(query, 10).scoreDocs;
			for (int i = 0; i < hitDocs.length; i++) {
				Document hitDoc = indexSearcher.doc(hitDocs[i].doc);
				System.out.println("id： " + hitDoc.get("id") + "  title:" + hitDoc.get("title") + "   author:"
						+ hitDoc.get("author") + "   desc:" + hitDoc.get("desc"));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			LuceneUtil.close(reader, directory);
		}
	}
	
	
	@Test
	public void testBooleanQuery(){
		Directory directory =null;
		DirectoryReader reader =null;
		try {
			  directory = LuceneUtil.getDirectory(Constants.DIRECTROY_PATH);
			  reader = LuceneUtil.getDirectoryReader(directory);
			IndexSearcher indexSearcher = LuceneUtil.getIndexSearcher(reader);
			BooleanClause booleanClause1=new BooleanClause(new TermQuery(new Term("author","孙悟空")), Occur.MUST);
			BooleanClause booleanClause2=new BooleanClause(new TermQuery(new Term("title","西游记")), Occur.MUST);
			BooleanQuery booleanQuery =new BooleanQuery.Builder().add(booleanClause1).add(booleanClause2).build();
			ScoreDoc[] hitDocs = indexSearcher.search(booleanQuery, 10).scoreDocs;
			for (int i = 0; i < hitDocs.length; i++) {
				Document hitDoc = indexSearcher.doc(hitDocs[i].doc);
				System.out.println("id： " + hitDoc.get("id") + "  title:" + hitDoc.get("title") + "   author:"
						+ hitDoc.get("author") + "   desc:" + hitDoc.get("desc"));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			LuceneUtil.close(reader, directory);
		}
	}
	
	@Test
	public void testTermRangeQuery(){
		Directory directory =null;
		DirectoryReader reader =null;
		try {
			  directory = LuceneUtil.getDirectory(Constants.DIRECTROY_PATH);
			  reader = LuceneUtil.getDirectoryReader(directory);
			IndexSearcher indexSearcher = LuceneUtil.getIndexSearcher(reader);
			TermRangeQuery query =  TermRangeQuery.newStringRange("title", "三国演义1", "三国演义3", true, true);
			ScoreDoc[] hitDocs = indexSearcher.search(query, 10).scoreDocs;
			for (int i = 0; i < hitDocs.length; i++) {
				Document hitDoc = indexSearcher.doc(hitDocs[i].doc);
				System.out.println("id： " + hitDoc.get("id") + "  title:" + hitDoc.get("title") + "   author:"
						+ hitDoc.get("author") + "   desc:" + hitDoc.get("desc"));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			LuceneUtil.close(reader, directory);
		}
	}
	
	@Test
	public void testPrefixQuery(){
		Directory directory =null;
		DirectoryReader reader =null;
		try {
			  directory = LuceneUtil.getDirectory(Constants.DIRECTROY_PATH);
			  reader = LuceneUtil.getDirectoryReader(directory);
			IndexSearcher indexSearcher = LuceneUtil.getIndexSearcher(reader);
			PrefixQuery query =  new PrefixQuery(new Term("title", "三国演义"));
			ScoreDoc[] hitDocs = indexSearcher.search(query, 10).scoreDocs;
			for (int i = 0; i < hitDocs.length; i++) {
				Document hitDoc = indexSearcher.doc(hitDocs[i].doc);
				System.out.println("id： " + hitDoc.get("id") + "  title:" + hitDoc.get("title") + "   author:"
						+ hitDoc.get("author") + "   desc:" + hitDoc.get("desc"));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			LuceneUtil.close(reader, directory);
		}
	}
	
	@Test
	public void testWildcardQuery(){
		Directory directory =null;
		DirectoryReader reader =null;
		try {
			  directory = LuceneUtil.getDirectory(Constants.DIRECTROY_PATH);
			  reader = LuceneUtil.getDirectoryReader(directory);
			IndexSearcher indexSearcher = LuceneUtil.getIndexSearcher(reader);
			//WildcardQuery query =  new WildcardQuery(new Term("title", "三*义?"));
			WildcardQuery query =  new WildcardQuery(new Term("title", "三?演义?"));
			ScoreDoc[] hitDocs = indexSearcher.search(query, 10).scoreDocs;
			for (int i = 0; i < hitDocs.length; i++) {
				Document hitDoc = indexSearcher.doc(hitDocs[i].doc);
				System.out.println("id： " + hitDoc.get("id") + "  title:" + hitDoc.get("title") + "   author:"
						+ hitDoc.get("author") + "   desc:" + hitDoc.get("desc"));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			LuceneUtil.close(reader, directory);
		}
	}
	
	
	
}
